#!/bin/bash

echo "关闭防火墙和selinux"
systemctl disable --now firewalld NetworkManager
setenforce 0
sed -ri '/^[^#]*SELINUX=/s#=.+$#=disabled#' /etc/selinux/config

echo "关闭 dnsmasq"
systemctl disable --now dnsmasq

echo "关闭swap"
swapoff -a && sysctl -w vm.swappiness=0
sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab

echo "安装wget git jq psmisc "
yum install epel-release -y
yum install wget git  jq psmisc -y

echo "你需要重新，请执行reboot"


echo "检查perl，这是内核安装依赖项"
[ ! -f /usr/bin/perl ] && yum install perl -y

echo "导入elrepo 的yum 源"
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm

echo "安装内核版本4.18.9-1"
export Kernel_Vsersion=4.18.9-1
  wget  http://mirror.rc.usf.edu/compute_lock/elrepo/kernel/el7/x86_64/RPMS/kernel-ml{,-devel}-${Kernel_Vsersion}.el7.elrepo.x86_64.rpm
yum localinstall -y kernel-ml*

echo "检查内核模块是否有nf_conntrack_ipv4"
find /lib/modules -name '*nf_conntrack_ipv4*' -type f

echo "修改内核启动顺序"
grub2-set-default  0 && grub2-mkconfig -o /etc/grub2.cfg

echo "下载docker检查的脚本check-config.sh"
curl https://raw.githubusercontent.com/docker/docker/master/contrib/check-config.sh > check-config.sh
bash ./check-config.sh

echo "修复docker检查脚本报错"
grubby --args="user_namespace.enable=1" --update-kernel="$(grubby --default-kernel)"

echo "你需要重新，请执行reboot"

echo "安装ipvs"
yum install ipvsadm ipset sysstat conntrack libseccomp -y

echo "加载内核模块"
:> /etc/modules-load.d/ipvs.conf
module=(
  ip_vs
  ip_vs_lc
  ip_vs_wlc
  ip_vs_rr
  ip_vs_wrr
  ip_vs_lblc
  ip_vs_lblcr
  ip_vs_dh
  ip_vs_sh
  ip_vs_fo
  ip_vs_nq
  ip_vs_sed
  ip_vs_ftp
  nf_conntrack
  br_netfilter
  )

for kernel_module in ${module[@]};do
    /sbin/modinfo -F filename $kernel_module |& grep -qv ERROR && echo $kernel_module >> /etc/modules-load.d/ipvs.conf || :
done
systemctl enable --now systemd-modules-load.service

cat <<EOF > /etc/sysctl.d/k8s.conf
# https://github.com/moby/moby/issues/31208 
# ipvsadm -l --timout
# 修复ipvs模式下长连接timeout问题 小于900即可
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 10
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
net.ipv4.neigh.default.gc_stale_time = 120
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2
net.ipv4.ip_forward = 1
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 1024
net.ipv4.tcp_synack_retries = 2
# 要求iptables不对bridge的数据进行处理
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-arptables = 1
net.netfilter.nf_conntrack_max = 2310720
fs.inotify.max_user_watches=89100
fs.may_detach_mounts = 1
fs.file-max = 52706963
fs.nr_open = 52706963
vm.swappiness = 0
vm.overcommit_memory=1
vm.panic_on_oom=0
EOF

sysctl --system


echo "最后设置hostname以及hosts文件，同步时区CST，节点时间不一致会出现问题"
yum install -y ntpdate
rm -f /etc/localtime 
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 
ntpdate us.pool.ntp.org


echo "安装docker"
wget https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/docker-ce-18.06.0.ce-3.el7.x86_64.rpm 
wget https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch.rpm

yum localinstall -y docker-ce-selinux*
yum localinstall -y docker-ce*.rpm

systemctl enable docker

mkdir -p /etc/docker/
cat > /etc/docker/daemon.json<<EOF
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "storage-driver": "overlay2",
  "graph": "/data/docker",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ],
  "registry-mirrors": ["https://fz5yth0r.mirror.aliyuncs.com"],
  "insecure-registries": ["harbor.sit.hupu.io"]
}
EOF
systemctl daemon-reload

cat >> /etc/hosts << EOF
10.64.57.168 harbor.tke-cloud.io
EOF

yum install -y epel-release bash-completion && cp /usr/share/bash-completion/completions/docker /etc/bash_completion.d/
systemctl enable --now docker


echo "安装kubeadm kubelet kubectl工具"
cat > /etc/yum.repos.d/k8s.repo<<EOF
[kubernetes]
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
EOF

yum makecache; yum install kubeadm-1.16.3 kubelet-1.16.3 kubectl-1.16.3 -y
yum makecache; yum install kubeadm-1.14.0 kubelet-1.14.0 kubectl-1.14.0 -y


# 解决kubelet自动联想问题
yum install bash-completion -y 
systemctl enable kubelet

# 搜索镜像
curl  -s https://zhangguanzhang.github.io/bash/pull.sh | bash -s -- search quay.io/calico/cni:v3.3.1 搜索对应的镜像

curl  -s https://zhangguanzhang.github.io/bash/pull.sh | bash -s -- quay.io/calico/cni:v3.3.1 下载对应的镜像

# 修改kubelet启动默认是systemd（v1.14.1）
/usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf中的/var/lib/kubelet/kubeadm-flags.env文件中修改如下
```yml
KUBELET_KUBEADM_ARGS=--cgroup-driver=systemd --network-plugin=cni --pod-infra-container-image=192.168.56.102:5000/google_containers/pause:3.1
```

kubeadm join 10.64.57.166:6443 --token dwtdsw.0123456789dwsfsa \
    --discovery-token-ca-cert-hash sha256:bb828ab6f32cc5cbd77b9b6c78dbf5b9b4f1f73f34e32dee611d6907634c6983


kubeadm token generate; kubeadm token create nepo8n.lig1zeo3pt2ugguw --print-join-command --ttl=0